package xyz.test;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import java.util.Calendar;

import xyz.dao.NoteDao;
import xyz.dao.NoteDatabase;
import xyz.entity.Note;
import xyz.time.DateAndTimePicker;
import xyz.utils.Util;
import xyz.viewmodel.NoteViewModel;

import static xyz.utils.Util.setNoteAlarm;

public class ShowNoteActivity extends AppCompatActivity {
    private int mFontSize = 1;
    private int mFontSizeTemp;
    private TextView mContentTv;
    private boolean mHaveAlarm = false;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_note_item);

        mContentTv = findViewById(R.id.tv);
        Toolbar toolbar = findViewById(R.id.toolbar);


        final TextView $title = findViewById(R.id.title);
        final TextView $text = findViewById(R.id.tv);


        final int id = getIntent().getIntExtra("id", 1);
        final LiveData<Note> note = this.getLiveNote(id);

        note.observe(this, new Observer<Note>() {
            @Override
            public void onChanged(Note note) {
                if (note != null) {
                    $title.setText(note.title);
                    $text.setText(note.content);
                }
            }
        });


        toolbar.inflateMenu(R.menu.toolbar_show_note);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.remain: {
                        if (!mHaveAlarm) {
                            DateAndTimePicker picker = new DateAndTimePicker(ShowNoteActivity.this,
                                    Calendar.getInstance());
                            picker.setOnMyTimeChangedListener(new DateAndTimePicker.OnMyTimeChangedListener() {
                                @Override
                                public void onSelected(Calendar calendar) {
                                    mHaveAlarm = true;
                                    Util.setNoteAlarm(ShowNoteActivity.this, id,
                                            calendar.getTimeInMillis());
                                }

                                @Override
                                public void onCanceled() {

                                }
                            });
                            picker.show();
                        } else {

                        }

                        break;
                    }
                    case R.id.edit: {
                        ShowNoteActivity.this.toastBuilder("edit");
                        Intent editIntent = new Intent(ShowNoteActivity.this, EditNoteActivity.class);
                        editIntent.putExtra("id", note.getValue().id);
                        editIntent.putExtra("state", EditNoteActivity.STATE_EDIT);
                        ShowNoteActivity.this.startActivity(editIntent);

                        break;
                    }

                    case R.id.delete_note: {
                        ShowNoteActivity.this.toastBuilder("delete_note");
                        break;
                    }
                    case R.id.font_size: {
                        mFontSizeTemp = mFontSize;
                        new AlertDialog.Builder(ShowNoteActivity.this)
                                .setTitle(R.string.font_dialog_title)
                                .setSingleChoiceItems(getResources().getStringArray(R.array.font_size_text), mFontSize, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int which) {
                                        mFontSizeTemp = which;
                                    }
                                })
                                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int which) {
                                        mFontSize = mFontSizeTemp;
                                        int[] fontsize = getResources().getIntArray(R.array.font_size);
                                        for (int i = 0; i < fontsize.length; i++) {
                                            if (i == mFontSizeTemp) {
                                                mContentTv.setTextSize(fontsize[i]);
                                            }
                                        }
                                    }
                                })
                                .setNegativeButton(android.R.string.cancel, null).show();
                        break;
                    }
                    case R.id.find: {
                        ShowNoteActivity.this.toastBuilder("find");
                        break;
                    }

                }

                return false;
            }
        });


    }

    private static final String TAG = "xyz:ShowNoteActivity";


    protected LiveData<Note> getLiveNote(int id) {
        return new ViewModelProvider(this).get(NoteViewModel.class).getOneNoteLiveData(id);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }


    void toastBuilder(String text) {
        Toast.makeText(ShowNoteActivity.this, text, Toast.LENGTH_SHORT).show();
    }
}